package com.extension.company.view.main;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.extension.company.common.mvp.BaseFragment;
import com.extension.company.contract.main.IncomeFragmentContract;
import com.extension.company.model.entity.IncomeData;
import com.extension.company.admin.R;

import com.extension.company.common.utils.Utils;
import com.extension.company.presenter.main.IncomeFragmentPresenter;
import com.extension.company.view.adapter.IncomeAdapter;
import com.extension.company.view.income.IncomeDetail;
import com.extension.company.view.income.MyBuyers;
import com.extension.company.view.income.MyFootprint;
import com.extension.company.view.income.PromotionOrder;
import com.extension.company.view.income.SalesDetail;
import com.extension.company.view.income.WithdrawCash;

import java.util.List;

import butterknife.BindView;

/**
 * 收益的fragment
 */
public class FragmentIncome extends BaseFragment<IncomeFragmentContract.View, IncomeFragmentPresenter> implements IncomeFragmentContract.View, View.OnClickListener {

	@BindView(R.id.m_top)
	LinearLayout m_top;
	@BindView(R.id.tv_cash)
	TextView tv_cash;
	@BindView(R.id.iv_photo)
	ImageView iv_photo;
	@BindView(R.id.layout_money_detail)
	LinearLayout layout_money_detail;
	@BindView(R.id.layout_myfootprint)
	LinearLayout layout_myfootprint;
	@BindView(R.id.layout_sales)
	LinearLayout layout_sales;
	@BindView(R.id.layout_porder)
	LinearLayout layout_porder;
	@BindView(R.id.layout_mybuyers)
	LinearLayout layout_mybuyers;
	@BindView(R.id.mRecyclerView)
	RecyclerView mRecyclerView;

	private List<IncomeData> list;
	private IncomeAdapter adapter;
	private Activity context;

	@Override
	public void onAttach(Context context) {
		super.onAttach(context);
		this.context = (Activity) context;
	}

	@Override
	public IncomeFragmentContract.View createView() {
		return this;
	}

	@Override
	public IncomeFragmentPresenter createPresenter() {
		return new IncomeFragmentPresenter();
	}

	@Override
	public void init() {
		initView();
		initData();
		initListener();
	}

	@Override
	public int getMainLayout() {
		return R.layout.fragment_income;
	}

	private void initView() {
		m_top = getViewById(R.id.m_top);
//        如果4.4及以上可以让状态栏透明
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) m_top.getLayoutParams();
			layoutParams.height = Utils.getStatusBarHeight(context);
			m_top.setLayoutParams(layoutParams);
			m_top.setBackgroundColor(context.getResources().getColor(R.color.black));
		}
		iv_photo.setFocusableInTouchMode(true);
//        LinearLayoutManager layoutManager = new LinearLayoutManager(activity);
//        layoutManager.setSmoothScrollbarEnabled(true);
//        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
		mRecyclerView.setLayoutManager(new GridLayoutManager(context, 2));
		mRecyclerView.setHasFixedSize(true);
		mRecyclerView.setNestedScrollingEnabled(false);
	}

	private void initData() {
		adapter = new IncomeAdapter(context, list);
		mRecyclerView.setAdapter(adapter);
	}

	private void initListener() {
		layout_money_detail.setOnClickListener(this);
		layout_myfootprint.setOnClickListener(this);
		layout_sales.setOnClickListener(this);
		layout_porder.setOnClickListener(this);
		layout_mybuyers.setOnClickListener(this);
		tv_cash.setOnClickListener(this);
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.layout_money_detail:
				IncomeDetail.startActivity(context);
				break;
			case R.id.layout_myfootprint:
				MyFootprint.startActivity(context);
				break;
			case R.id.layout_sales: 	//销售详情

				SalesDetail.startActivity(context);
				break;
			case R.id.layout_porder:
				PromotionOrder.startActivity(context);
				break;
			case R.id.layout_mybuyers: //我的买家
				MyBuyers.startActivity(context);
				break;
			case R.id.tv_cash:                //提现
				WithdrawCash.startActivity(context);
				break;
			default:
				break;
		}
	}

	@Override
	public void showLoading() {

	}

	@Override
	public void hideLoading() {

	}

	@Override
	public void onError(Throwable throwable) {

	}
}
